package com.yidin.lsc;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public abstract class Views {
	public Views(Activity activity) {
		this.activity = activity;
		LayoutInflater inflater = activity.getLayoutInflater();
		container = inflater.inflate(R.layout.viewcontainer, null);
		views = new ArrayList<View>();
		this.showFunctionsListView();
		this.findView();
		//this.initialize();
	}

	public View getActiveView() {
		return container;
	}

	protected View addView(int resource) {
		LayoutInflater inflater = activity.getLayoutInflater();
		View view = inflater.inflate(resource, null);
		ViewGroup group = (ViewGroup) container
				.findViewById(R.id.ViewContainer);
		group.removeAllViews();
		group.addView(view, new ViewGroup.LayoutParams(-1, -1));
		views.add(view);
		return view;
	}

	protected boolean removeView() {
//		int size = views.size();
//		if (size > 0) {
//			ViewGroup group = (ViewGroup) container
//					.findViewById(R.id.ViewContainer);
//			group.removeAllViews();
//			//group.addView(views.get(size - 1));
//			group.clearFocus();
//			group.requestFocus();
//			
//			//views.get(size - 2).getFocusables(0);
//			views.remove(size - 1);
//			size = views.size();
//			return true;
//		}
		
		int size = views.size();
		if (size > 1) {
			ViewGroup group = (ViewGroup) container
					.findViewById(R.id.ViewContainer);
			group.removeAllViews();
			group.addView(views.get(size - 2));
			group.clearFocus();
			group.requestFocus();
			
			views.get(size - 2).getFocusables(0);
			views.remove(size - 1);
			return true;
		}
		return false;
	}

	/**
	 * 获取 XML 文件内容
	 * 
	 * @param inStream
	 *            获取的输入流
	 */
	protected void parseXml(InputStream inStream, DefaultHandler dh,
			Handler handler) {
		SAXParserFactory factory = SAXParserFactory.newInstance();
		SAXParser parser;
		try {
			parser = factory.newSAXParser();
			XMLReader reader = parser.getXMLReader();
			reader.setContentHandler(dh);
			try {
				parser.parse(inStream, dh);
				Message message = handler.obtainMessage(0, null);
				handler.sendMessage(message);
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	protected Activity activity;
	private View container;
	private List<View> views;
	/**
	 * 后台服务返回的数据
	 * @param param
	 */
	public abstract void refresh(Object... param);
	/**
	 * 初始化网络数据
	 */
	public abstract void checkCache();
	public abstract void findView();
	public abstract void showFunctionsListView();
	public abstract void initialize();
	public void setCurrionPage(int page, int res) {
		
	}
}